package com.cheyun.netsalev3.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR
import androidx.lifecycle.ViewModel
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.BlankLayoutBinding
import com.cheyun.netsalev3.utils.api.NetworkState

/**
 * 通用分页适配器
 */

open class MyPagedListAdapter<T>(open var layoutId: Int,
                                 open var viewModel: ViewModel?,
                                 open var callback: ItemCallback<T>? = ItemCallback()) :
        PagedListAdapter<T, com.cheyun.netsalev3.adapter.MyPagedListAdapter.ViewHolder>(callback!!) {

    constructor(id: Int) : this(id, null)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return if (viewType == 1) {
            ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), layoutId, parent, false))
        } else {
            ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.blank_layout, parent, false))
        }
    }

    override fun onBindViewHolder(holder: com.cheyun.netsalev3.adapter.MyPagedListAdapter.ViewHolder, position: Int) {
        var binding = holder.binding
        if (binding is BlankLayoutBinding) {

            if (itemCount == 1 && position == 0) {
                var layout =
                        ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                binding.rootView.layoutParams = layout
            } else {
                var layout =
                        ViewGroup.LayoutParams(0, 0)
                binding.rootView.layoutParams = layout
            }
        } else {
            var param = getItem(position)
            holder.apply {
                bind(param, viewModel)
                itemView.tag = param
            }
        }
    }

    private var networkState: NetworkState? = null
    private fun hasExtraRow() = networkState != null && networkState == NetworkState.LOADED

    override fun getItemCount(): Int {
        return super.getItemCount() + if (hasExtraRow()) 1 else 0
    }

    fun setNetworkState(newNetworkState: NetworkState?) {
        Log.e("newNetworkState", newNetworkState.toString())
//        val previousState = this.networkState
//        val hadExtraRow = hasExtraRow()
        this.networkState = newNetworkState
        val hasExtraRow = hasExtraRow()
//        if (hadExtraRow != hasExtraRow) {
        if (hasExtraRow) {
            notifyItemRemoved(super.getItemCount())
        } else {
            if (super.getItemCount() == 0) {
//              notifyItemInserted(super.getItemCount())
            } else {
                this.networkState = NetworkState.LOADING
                notifyItemRemoved(super.getItemCount())
            }
        }
//        }
//    else if (hasExtraRow && previousState != newNetworkState) {
//            notifyItemChanged(itemCount - 1)
//        }
    }


    override fun getItemViewType(position: Int): Int {
        return if (hasExtraRow() && position == itemCount - 1) {
            2//空白无数据
        } else {
            1
        }
    }


    class ViewHolder(var binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Any?, viewModel: ViewModel?) {
            binding.apply {
                setVariable(BR.item, item)
                setVariable(BR.viewModel, viewModel)
                setVariable(BR.binding, this)
                setVariable(BR.position, adapterPosition)
                executePendingBindings()
            }
        }
    }


    /**
     * 默认的比较器 可以覆盖比较方法
     */
    open class ItemCallback<T> : DiffUtil.ItemCallback<T>() {
        override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
            return oldItem == newItem
        }
    }
}